package com.example.demo;


import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;


public class LoginActivity extends AppCompatActivity {

    private EditText editTextUsername;
    private EditText editTextPassword;
    private TextView textViewError;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        editTextUsername = findViewById(R.id.editTextUsername);
        editTextPassword = findViewById(R.id.editTextPassword);
        textViewError = findViewById(R.id.textViewError);
        Button buttonLogin = findViewById(R.id.buttonLogin);


        buttonLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = editTextUsername.getText().toString();
                String password = editTextPassword.getText().toString();
                if (username.isEmpty() || password.isEmpty()) {
                    showError("Please fill in all fields");
                } else {
                    // 调用后台服务器验证逻辑
                    validateLoginWithServer(username, password);
                }
            }
        });
    }


    private void validateLoginWithServer(String username, String password) {
        OkHttpClient client = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
              .add("username", username)
              .add("password", password)
              .build();
        Request request = new Request.Builder()
              .url("http://your-server-url/login") // 请将此 URL 替换为实际的服务器登录地址
              .post(requestBody)
              .build();


        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        showError("Network error. Please try again later.");
                    }
                });
            }


            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                        Toast.makeText(LoginActivity.this, "Login successful", Toast.LENGTH_SHORT).show();
                            startActivity(new Intent(LoginActivity.this, MainActivity.class));
                        }
                    });
                    } else {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                        showError("Invalid username or password");
                    }
                    });
                }
            }
        });
    }


    private boolean validateLogin(String username, String password) {
        // 这里可以添加实际的验证逻辑，例如与本地存储或服务器验证
        // 这里仅做简单示例，假设用户名是 "admin"，密码是 "123456" 为合法登录
        if (username.equals("admin") && password.equals("123456")) {
            return true;
        }
        return false;
    }


    private void showError(String message) {
        textViewError.setVisibility(View.VISIBLE);
        textViewError.setText(message);
    }
}